###############################################################################
# PHP 5.5 fastcgi settings
# Should set the document root in server {}
# @author Jiankai Zeng <janpoem@163.com>
###############################################################################

# 基础屏蔽

location ~ /\. {
        return 404;
}

location ~* \.(pem|sql|mysql|ini|conf)$ {
        return 404;
}

# SQL 注入

set $block_sql_injections 0;

if ($query_string ~ "union.*select.*\(") {
        set $block_sql_injections 1;
}
if ($query_string ~ "union.*all.*select.*") {
        set $block_sql_injections 1;
}
if ($query_string ~ "concat.*\(") {
        set $block_sql_injections 1;
}
if ($block_sql_injections = 1) {
        return 500;
}

# 文件注入

set $block_file_injections 0;

#if ($query_string ~ "[a-zA-Z0-9_]=http://") {
#        set $block_file_injections 1;
#}
if ($query_string ~ "[a-zA-Z0-9_]=\/?(\.\.\/?|\.\/?)+") {
        set $block_file_injections 1;
}
#if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") {
#        set $block_file_injections 1;
#}
if ($block_file_injections = 1) {
        return 500;
}

# 禁止溢出攻击

set $block_common_exploits 0;

if ($query_string ~ "(<|%3C).*script.*(>|%3E)") {
        set $block_common_exploits 1;
}
if ($query_string ~ "(GLOBALS|SESSION|_GET|_POST)(=|\[|\%[0-9A-Z]{0,2})") {
        set $block_common_exploits 1;
}
if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") {
        set $block_common_exploits 1;
}
if ($query_string ~ "proc/self/environ") {
        set $block_common_exploits 1;
}
if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") {
        set $block_common_exploits 1;
}
if ($query_string ~ "base64_(en|de)code\(.*\)") {
        set $block_common_exploits 1;
}
if ($block_common_exploits = 1) {
        return 500;
}

# user agents屏蔽

set $block_user_agents 0;

# Disable Akeeba Remote Control 2.5 and earlier
if ($http_user_agent ~ "Indy Library") {
        set $block_user_agents 1;
}

# Common bandwidth hoggers and hacking tools.
if ($http_user_agent ~ "libwww-perl") {
        set $block_user_agents 1;
}
if ($http_user_agent ~ "GetRight") {
        set $block_user_agents 1;
}
if ($http_user_agent ~ "GetWeb!") {
        set $block_user_agents 1;
}
if ($http_user_agent ~ "Go!Zilla") {
        set $block_user_agents 1;
}
if ($http_user_agent ~ "Download Demon") {
        set $block_user_agents 1;
}
if ($http_user_agent ~ "Go-Ahead-Got-It") {
        set $block_user_agents 1;
}
if ($http_user_agent ~ "TurnitinBot") {
        set $block_user_agents 1;
}
if ($http_user_agent ~ "GrabNet") {
        set $block_user_agents 1;
}
if ($http_user_agent ~ "WebBench") {
        set $block_user_agents 1;
}
if ($http_user_agent ~ "ApacheBench") {
        set $block_user_agents 1;
}
if ($http_user_agent ~ ^$) {
        set $block_user_agents 1;
}
if ($http_user_agent ~ ^-$){
        set $block_user_agents 1;
}
if ($http_user_agent ~ "Python-urllib") {
        set $block_user_agents 1;
}
if ($block_user_agents = 1) {
        return 500;
}